<?php
/*
# ------------------------------------------------------------------------
# JA Teline III - Version 1.3 - Licence Owner jSharing.COM
# ------------------------------------------------------------------------
# Copyright (C) Lifetime Joomla Just for Sharing, Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: Joomla Just for Sharing
# Websites:  http://jsharing.com -  http://jsharing.net
# This file may not be redistributed in whole or significant part.
# ------------------------------------------------------------------------
*/ 
/**
 * JA Bookmark Plugin is a module using for Display icons for your online social networking sites.
 */
// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die();
// include libs.
jimport( 'joomla.plugin.plugin' );
jimport('joomla.application.module.helper');

 /**
  * plgContentPlg_Jabookmark class
  */
class plgContentPlg_Jabookmark extends JPlugin {
	
	/**
	 * @var JParameter $params.
	 *
	 * @access public.
	 */
	var $params = null;
		
	/**
	 * @var stdClass store Plugin Information
	 *
	 * @access public
	 */
	var $plugin;
	
	/**
	 * var $string $__output store html output.
	 * 
	 * @access private
	 */
	var $__output = null; 
	
	/**
	 * constructor.
	 */
	function plgContentPlg_Jabookmark( &$subject, $params=null ){
		if (!$subject) return;
		parent::__construct( $subject, $params );
		
		$this->plugin	=& JPluginHelper::getPlugin('content', 'plg_jabookmark');
		$this->params	= new JParameter( $this->plugin->params );
	}
		
	/**
	 * check valid showing addthis service.
	 *
	 * @param JParameter $params.
	 * @param stdClass $article.
	 * @return boolean.
	 */
	function isValidShowing( $params, $article ){
	
		global $option, $mainframe;
		
		if ( $option != "com_content" ) return false;
		$app = JFactory::getApplication(); 
		if (  $app->scope != 'com_content') {
            return  false;
        }
		// and only procces in the article page.
		$view 	= JRequest::getCmd( "view" );
		if(  strtolower($view) != 'article' ){
			return false;
		} 
		// check custom_display_in mode
		if( $params->get('category', '') == '' ){
			return true;
		} else {
			$categories = $params->get('category');
			if( !is_array($categories) ) {
				$categories = split( ',', $params->get('category') );
			}
			if( !empty($categories) ){
				return in_array( $article->catid, $categories );	
			}
		}
		return true;
	}
	
	/**
	 * only render button some postions such as TOP, BOTTOM, CUSTOM LOCATION.
	 */
	function onPrepareContent( &$article, &$params, $page=0 ){

		if( preg_match('/onPrepareContent-(\w+)/', $this->params->get('location'), $out) ) {		
			switch( $out[1] ){
				case "Bottom":
					$article->text .= $this->render( $this->params, $article );
					break;
				case "Custom":
					$article->jabookmark = $this->render( $this->params, $article );
					break;
				default:
					$text = $this->render( $this->params, $article ). $article->text;
					$article->text = $text;
					unset( $text );
					break;
			}	
		}
		return true;
	}
	
	/**
	 * only render button at postion after content.
	 */
	function onAfterDisplayContent( &$article, &$params, $page=0 ){
		if( $this->params->get('location') == 'onAfterDisplayContent' ){ 
			$text = $this->render( $this->params, $article );	
			return $text;
		}
	}
	
	/**
	 * only render button at postion before content.
	 */
	function onBeforeDisplayContent( &$article, &$params, $page=0 ){
		if( $this->params->get('location') == 'onBeforeDisplayContent' ){
			$text = $this->render( $this->params, $article );	
			return $text;
		}
	}
	
	function onAfterDisplayTitle( &$article, &$params, $page=0 ){
		if( $this->params->get('location') == 'onAfterDisplayTitle' ){
			$text = $this->render( $this->params, $article );	
			return $text;
		}
	}	
	/**
	 * processing render service layout.
	 */
	function onAfterRender(){
		if( $this->__output != null ){
			$body = JResponse::getBody();
			$body = str_replace( '</body>', $this->__output ."\n</body>", $body );
			JResponse::setBody($body);
			unset( $this->__output );
		}
	}
	
	/**
	 * render button service 
	 * 
	 * @param JParameter $params
	 * @array stdClass $article
	 * @return string.
	 */
	function render( $params, &$article ){ 
		$overrideLocation = false;
		// only show JA BOOKMARK with content components and article view.
		if( !($this->isValidShowing($params, $article)) ) { 
			$article->text = $this->removeConfigString( $article->text );
			return ;
		}
		// get configuration string from Article's content.
		$configs = $this->parserConfigString( $article->text );	
		// if use "turnoff" mode
		$mode = $params->get('mode');
		if( $mode == 'turnoff'){
			$article->text = $this->removeConfigString( $article->text );
			return '';
		}
		else if( $mode == 'manual' && ( $configs == null || empty($configs)) ){
			return '';		
		}
		//find "off" mode in a article  
		if( isset($configs[0][0]) ){
			if( strpos($configs[0][0], "off") ){
				$article->text = $this->removeConfigString( $article->text );
				return '';	
			} else {
				$overrideLocation = true;		
			}
		} 
		// load css file
		// $this->stylesheet();
		$system =  $params->get( 'system', 'addthis' );
		// get HTML output from system view
		$content_layout = $this->renderLayout( $system, compact('system', 'configs') );
		// render a centain layout and this system view
		$this->__output = $this->renderLayout( 'common', compact('content_layout') );
	
		// it's wapper would be replace by other.	
		$wapper = '<div id="jabookmarkWapper"></div>';
		// if using override location, this function would be apply for all "display mode" options.
		if( $overrideLocation ){
			$article->text = $this->removeConfigString($article->text, $wapper);
			return '';
		} else {
	 		return $wapper;	
		}
		
	}
	
	/**
	 * render layout, allow override layout by owner.
	 *
	 * @param stdClass $plugin plugin information 
	 * @param string layout
	 * @return string
	 */
	function getLayoutPath( $plugin, $layout = 'default' ){
		global $mainframe;
		// Build the template and base path for the layout
		$tPath = JPATH_BASE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.$plugin->name.DS.$layout.'.php';
		$bPath = JPATH_BASE.DS.'plugins'.DS.$plugin->type.DS.$plugin->name.DS.'tmpl'.DS.$layout.'.php';
		// If the template has a layout override use it
		if (file_exists($tPath)) {
			return $tPath;
		} elseif (file_exists($bPath)) {
			return $bPath;
		}
		return '';
	}
	
	/**
	 * render layout.
	 *
	 * @param string $layout layout's name.
	 * @param array data passed.
	 * @return string is HTML.
	 */
	function renderLayout( $layout, $data=array() ){
	
		extract( $data );
		// include  processor file to render User inteface
		$pathFile =  $this->getLayoutPath( $this->plugin, $layout ); 
		if( $pathFile != '' ){
			ob_start();
			require_once( $pathFile );
			$content = ob_get_contents();
			ob_end_clean();
			
			return $content;
		}
		return '';
	}
	
	function stylesheet ( $plugin ) {
		global $mainframe;
		JHTML::stylesheet('style.css','plugins/'.$plugin->type.'/'.$plugin->name.'/');
		if (is_file(JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'css'.DS.$plugin->name.".css"))
		JHTML::stylesheet($plugin->name.".css",'templates/'.$mainframe->getTemplate().'/css/');
	} 
	
	/**
	 * parser string in the article's content, check having configuration social existed?
	 *
	 * @param string $text.
	 * @return array if having, equal null if not.
	 */
	function parserConfigString( $text ){ 
		if( preg_match("#{jabookmark(.*)}#s", $text, $matches, PREG_OFFSET_CAPTURE) ){ 
			return $matches;
		}	
		return null;
	}
	
	/**
	 * remove configuration string in the artilce's content.
	 *
	 * @param string $text.
	 * @return boolean
	 */
	function removeConfigString( $text, $string='' ){
		return preg_replace( '#{jabookmark(.*)}#s', $string, $text );
	}
}
?>